package com.wawu.server.dao;


import com.wawu.common.annotation.IOC.component.Repository;
import com.wawu.common.exception.BaseException;
import com.wawu.common.utils.SQLBuilder.SelectSQLBuilder;
import com.wawu.common.utils.SQLBuilder.UpdateSQLBuilder;
import com.wawu.common.utils.SQLExcutorUtil;
import com.wawu.pojo.entity.Comment;
import com.wawu.pojo.entity.Message;
import com.wawu.pojo.entity.Order;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class CommentDAO {


    public List<Comment> getCommentsById(Long id) throws SQLException {
        SelectSQLBuilder sqlBuilder=new SelectSQLBuilder("comments")
                .addDynamicCondition("id","=", id)
                .addOrderBy("createtime","DESC");
        return SQLExcutorUtil.queryList(sqlBuilder.getSQL(),Comment.class,sqlBuilder.getParams());
    }

    public List<Comment> getCommentsByGoodsId(Long id) throws SQLException {
        SelectSQLBuilder sqlBuilder=new SelectSQLBuilder("comments")
                .addDynamicCondition("goodsId","=", id)
                .addOrderBy("createtime","DESC");
        return SQLExcutorUtil.queryList(sqlBuilder.getSQL(),Comment.class,sqlBuilder.getParams());
    }

    /**
     * 根据订单id判断是否有评论
     * @param OrderId
     * @return
     */
    public boolean hasComment(Long OrderId) throws SQLException {
        String sql="select 1 from comments where orderId=? limit 1";
        Long has = SQLExcutorUtil.queryScalar(sql, Long.class, OrderId);
        return has!=null;
    }


}
